﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.TextEditor.Document;
using DBSourceToolsLib.Composite;
using DBSourceToolsLib.Suggestions;

namespace ICSharpCode.MSSQLExtensions
{
    public class MSSQLTextEditor : ICSharpCode.TextEditor.TextEditorControl
    {
        System.Timers.Timer m_Timer = new System.Timers.Timer();
        //DBSourceToolsLib.Suggestions.SuggestionBuilder oBuilder;
        //private HCCodeParser.SourceFile m_SourceFile;
        private System.Windows.Forms.Form m_ParentForm;

        //public HCCodeParser.SourceFile SourceFile
        //{
        //    get { return m_SourceFile; }
        //}
        
        private HashSet<char> m_HashFoldingStrings;
        private object m_LockFoldingTimer = new object();
        private bool m_bDoFoldings = false;

        public IDocument IDocument { get { return base.Document; } }

        public MSSQLTextEditor(System.Windows.Forms.Form oParent)
        {
            m_ParentForm = oParent;
            //m_SourceFile = new HCCodeParser.SourceFile(base.Document);
            //m_HashFoldingStrings = m_SourceFile.GetHashSet();
            base.Document.HighlightingStrategy 
                = HighlightingStrategyFactory.CreateHighlightingStrategy("MSSQL");
            base.Document.FoldingManager.FoldingStrategy = new MSSQLFoldingStrategy();

            // gap text buffer is not thread safe. 
            // this is a workable solution.
            m_Timer.Interval = 1000; // 1.5 seconds
            m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(m_Timer_Elapsed);
            m_Timer.Enabled = true;

            base.ActiveTextAreaControl.TextArea.KeyUp += new System.Windows.Forms.KeyEventHandler(TextArea_KeyUp);
            
        }

        public void SetComposite(IDBComposite oComposite)
        {
            ICSharpCode.MSSQLExtensions.HCSqlCodeCompletionKeyHandler.Attach(m_ParentForm, this, oComposite);

            HighlightRuleSet oRuleSet = base.Document.HighlightingStrategy.GetDefaultRuleSet();
            
            HighlightColor oFieldColor = new HighlightColor(
                System.Drawing.Color.FromArgb(43,145,175), false, false);

            HighlightColor oTableColor = new HighlightColor(
                System.Drawing.Color.BlueViolet, false, false);

            HighlightColor oSchemaColor = new HighlightColor(
                System.Drawing.Color.MediumOrchid, false, false);


            foreach (string strTable in SuggestionConverter.SuggestionListToSimple(
                (oComposite.BuildList(FieldListType.Short, DBCompositeType.Table, DBCompositeType.View)))
                )
                oRuleSet.KeyWords[strTable] = oFieldColor;

            foreach (string strTable in SuggestionConverter.SuggestionListToSimple(
            (oComposite.BuildList(FieldListType.Short, DBCompositeType.Field))))
            {
                oRuleSet.KeyWords[strTable] = oFieldColor;
            }

            foreach (string strTable in SuggestionConverter.SuggestionListToSimple(
            (oComposite.BuildList(FieldListType.Short, DBCompositeType.Schema))))
            {
                oRuleSet.KeyWords[strTable] = oTableColor;
            }
        }

        void TextArea_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //if ((e.KeyCode == System.Windows.Forms.Keys.Back) ||
            //(e.KeyCode == System.Windows.Forms.Keys.Delete) ||
            //(e.KeyCode == System.Windows.Forms.Keys.Enter) ||
            //    (m_HashFoldingStrings.Contains(e.KeyCode.ToString().ToLower()[0])))
            //{
            //    FlagFoldingUpdate();
            //}
        }

        public void FlagFoldingUpdate()
        {
            lock (m_LockFoldingTimer)
            {
                m_Timer.Stop();
                m_Timer.Start();
                m_bDoFoldings = true;
            }
        }

        void m_Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (m_LockFoldingTimer)
            {
                if (m_bDoFoldings == true)
                {
                    //DateTime oBegin = DateTime.Now;
                    base.Document.FoldingManager.UpdateFoldings(null, null);

                    base.ActiveTextAreaControl.TextArea.Invalidate();
                    //System.Threading.Thread.Sleep(100);
                    //DateTime oEnd = DateTime.Now;
                    //HCSocketToolsLib.HCSocketSender.Instance().SendUDPMessage(
                    //    System.Reflection.MethodBase.GetCurrentMethod()
                    //    , string.Format("Update Foldings -{0}s {1}ms ", (oEnd - oBegin).Seconds, (oEnd - oBegin).TotalMilliseconds));

                }
                m_bDoFoldings = false;

            }

        }


        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // textAreaPanel
            // 
            this.textAreaPanel.Size = new System.Drawing.Size(104, 104);
            // 
            // MSSQLTextEditor
            // 
            this.Name = "MSSQLTextEditor";
            this.Size = new System.Drawing.Size(104, 104);
            this.ResumeLayout(false);

        }

    }
}
